<?php

/*
 * This file is the log in page for the Green Route.
 * 
 * Created by: Julian Alfonso Ponce
 * 
 * Modified by: Peter Agno Jr.
 * Date Modified: September 11, 2011
 */

    include('includes/siteConfig.php'); 
?>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/css-frames-style.css" />
        
        <!--JAVASCRIPT SOURCES-->
        <script src="js/jquery-1.6.2.min.js"></script>
        <!--ACTUAL JS SCRIPT-->
        <script type="text/javascript">
            
            jQuery(document).ready(function(){ 
                
                jQuery("#inputUserName").focus();
                
                // START - Ajax for checking the validity of username and password
                jQuery(".inputbox").keypress(function(e){
                   if(e.keyCode == 13) {
                        jQuery("#submitButton").click();
                   }
                   
                });
                jQuery('#submitButton').live("click",function() {
                    var tempUsername, tempPassword, errorMsg="";
                                        
                    if ( jQuery("#inputUserName").val() == "" && jQuery("#inputPassword").val() == "" )
                        errorMsg = "Please input username and password.";
                    else {
                        if(jQuery("#inputUserName").val() == "")
                            errorMsg = "Please input username.";
                        else {
                            if (jQuery("#inputPassword").val() == "")
                                errorMsg = "Please input password.";
                        }
                    }

                    if (errorMsg!= ""){
                        jQuery(".errorMessage").html(errorMsg);
                    }
                    else{
                        
                    // Validate if username and password are correct in the database.
                        jQuery.ajax({
                            type : 'POST',
                            url : 'ajaxRequests/checkUserLogin.php',
                            dataType : 'json',
                            data: {
                                tempUsername : jQuery("#inputUserName").val(),
                                tempPassword : jQuery("#inputPassword").val()
                            },
                            success : function(data){
                                //Below is an example of showing multiple returns from an ajax using jQuery.
                                //alert(data.msg + " " + data.name);
                                
                                if(data.msg == "valid"){
                                    window.location = 'dashboard.php';
                                }
                                else{
                                    jQuery(".errorMessage").html(data.msg);
                                    
                                }
                            },
                            error : function() {
                                alert("fail");
                            }
                        });
                    }
                }); 
                // END - Ajax for checking the validity of username and password
                
            });
            
        </script>
    </head>
    
    <body>
<!--        <div style="width:540px; background:url(images/loginbg.gif);  align:center; height:255px; padding:10px;">-->
        <div class="loginDiv">
            <div align="center">
                <table>
                    <tr class="loginCssHeader">
                        <td align="center" colspan="2">Green Route</td>
                    </tr>
                    <tr>
                        <td colspan="2"><hr /></td>
                    </tr>
                    <tr class="loginExtraSpaceHeader">
                        <td colspan="2">A Document Tracking System for DLSC's Purchasing Office</td>
                    </tr>
                    <tr>
                        <td colspan="2">Username</td>
                    </tr>
                    <tr class="loginExtraSpace">
                        <td colspan="2"><input type="text" name="inputUserName" id="inputUserName" class="loginInput inputbox" size="16" /></td>
                    </tr>

                    <tr>
                        <td colspan="2">Password</td>
                    </tr>
                    <tr class="loginExtraSpace">
                        <td colspan="2"><input type="password" name="inputPassword" id="inputPassword" class="loginInput inputbox" size="16"/></td>
                    </tr>

                    <tr class="loginExtraSpace">
                        <td><span class="errorMessage"></span></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td align="left">
                            <input type="submit" value="Login" id="submitButton" class="siteGreenButton"/>
                        </td>
                        <td></td>
                    </tr>
<!--                    <tr class="loginExtraSpace">
                        <td align="right" colspan="2">
                            <input type="submit" value="LOGIN" id="submitButton" class="siteGreenButton"/>
                        </td>
                    </tr>-->
                </table>
            </div>
        </div>
<!--        </div>-->
    </body>
</html>
